﻿using Microsoft.Win32;
using System;
using System.Diagnostics;

namespace ASDL
{
    internal class ASDLHelper
    {
        //ASDL在注册表中的存放位置，这个是针对WinXP的，不知道Win7是否是这个，待验证
        private static String _adlskeys = @"RemoteAccess\Profile";

        public static String adlskeys
        {
            get
            {
                return _adlskeys;
            }
        }

        /// <summary>
        /// 获取本机的拨号名称，也就是本机上所有的拨号
        /// </summary>
        /// <returns></returns>
        public static String[] GetASDLNames()
        {
            RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(adlskeys);
            if (RegKey != null)
                return RegKey.GetSubKeyNames();
            else
                return null;
        }

        private String _asdlname = null;
        private ProcessWindowStyle _windowstyle = ProcessWindowStyle.Hidden;

        /// <summary>
        /// 实例化一个ASDL连接
        /// </summary>
        /// <param name="asdlname">ASDL名称，如“宽带连接”</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="windowstyle">窗口显示方式，默认为因此拨号过程</param>
        public ASDLHelper(String asdlname, String username = null, String password = null, ProcessWindowStyle windowstyle = ProcessWindowStyle.Hidden)
        {
            this.ASDLName = asdlname;
            this.Username = username;
            this.Password = password;
            this.WindowStyle = windowstyle;
        }

        /// <summary>
        /// 拨号名称
        /// </summary>
        public String ASDLName
        {
            get
            {
                return this._asdlname;
            }
            set
            {
                this._asdlname = value;
            }
        }

        /// <summary>
        /// 拨号进程的窗口方式
        /// </summary>
        public ProcessWindowStyle WindowStyle
        {
            get
            {
                return this._windowstyle;
            }
            set
            {
                this._windowstyle = value;
            }
        }

        private String _username = null;    //用户名
        private String _password = null;    //密码

        /// <summary>
        /// 用户名
        /// </summary>
        public String Username
        {
            get
            {
                return this._username;
            }
            set
            {
                this._username = value;
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        public String Password
        {
            get
            {
                return this._password;
            }
            set
            {
                this._password = value;
            }
        }

        /// <summary>
        /// 开始拨号
        /// </summary>
        /// <returns>返回拨号进程的返回值 0为成功</returns>
        public int Connect()
        {
            string args = string.Format("rasdial \"{0}\" {1} {2}", this.ASDLName, this.Username, this.Password);
            string res = InvokeCmd(args);
            //Console.WriteLine(res);
            //C: \Users\Wrg\Desktop\TouTiao\TouTiao\bin\Debug > rasdial "宽带连接" 051310510100 11223344
            //正在连接到 宽带连接...
            //正在验证用户名及密码...
            //正在网络上注册您的计算机...
            //已连接 宽带连接。
            //命令已完成。
            //C: \Users\Wrg\Desktop\TouTiao\TouTiao\bin\Debug > exit
            return 0;
            //using (Process pro = new Process())
            //{
            //    pro.StartInfo.FileName = "rasdial.exe";
            //    pro.StartInfo.Arguments = this.ASDLName + " " + this.Username + " " + this.Password;
            //    pro.StartInfo.WindowStyle = this.WindowStyle;
            //    pro.Start();
            //    pro.WaitForExit();
            //    return pro.ExitCode;
            //}
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        /// <returns></returns>
        public int Disconnect()
        {
            string args = string.Format("rasdial \"{0}\" /disconnect", this.ASDLName);
            string res = InvokeCmd(args);
            //Console.WriteLine(res);
            return 0;
            //C: \Users\Wrg\Desktop\TouTiao\TouTiao\bin\Debug > rasdial "宽带连接" / disconnect
            //命令已完成。
            //C: \Users\Wrg\Desktop\TouTiao\TouTiao\bin\Debug > exit

            //using (Process pro = new Process())
            //{
            //    pro.StartInfo.FileName = "rasdial.exe";
            //    pro.StartInfo.Arguments = this.ASDLName + " /DISCONNECT";
            //    pro.StartInfo.WindowStyle = this.WindowStyle;
            //    pro.Start();
            //    pro.WaitForExit();
            //    return pro.ExitCode;
            //}
        }

        private string InvokeCmd(string cmdArgs)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(cmdArgs);
            p.StandardInput.WriteLine("exit");
            return p.StandardOutput.ReadToEnd();
        }
    }
}